//砍价活动
var app = getApp(),
  a = app.requirejs("core"),
  info = app.getCache("userinfo"),
  s = app.requirejs("wxParse/wxParse");
module.exports = {
 
  /**
   * 首页轮播图、分类
   */
  BarIndex: function (obj) {
    var e = {};
    a.get('Bargain/BarIndex', {
    }, function (t) {
      console.log(t)
      if (t.code == 0) {
        e.cate_info = t.info.cate_info;
        e.carousel = t.info.carousel;
        typeof obj == "function" && obj(e)
      } else {
        a.alert(t.msg)
      }
    })
  },
  /**
   * 砍价活动商品列表
   */
  kj_list: function (cate_id, kj_type, page, that, obj) {
    var e = {};
    a.get('Bargain/Bargain', {
      type: kj_type,
      class_id: cate_id,
      page: page
    }, function (t) {
      console.log(t)
      if (t.code == 0) {
        t.info.forEach(function (i) {
          var short = i.star_time - Date.parse(new Date()) / 1000;
          if (short > 0) {
            i.goods_type = 1  //未开始
          } else {
            i.goods_type = 2 //已开始
          }
          i.user_num = i.user.length;
        })
        t.info.length > 0 && (e.page = page + 1, e.list = that.data.list.concat(t.info), t.info.length < 10 && (e.loaded = true));
        t.info.length == 0 && (e.loaded = true);
        e.show = true;
        typeof obj == "funciton" & obj(e)
      } else {
        a.alert(t.msg, function () {
          return;
        })
      }
    }, true)
  },
  /**
   * 商品详情
   */
  kj_detail: function (id, that, obj) {
    var e = {};
    a.get("Bargain/GoodsInfo", {
      id: id,
      uid: getApp().getCache("userinfo").uid,
    }, function (t) {
      console.log(t)
      if (t.code == 0 && t.info != null) {
        s.wxParse("wxParseData", "html", t.info.bargain_info.activity_rules, that, "0");
        e.bargain_info = t.info.bargain_info;
        e.about_info = t.info.about_info;
        e.show = true;
        e.show_chat = t.info.show_chat;
        typeof obj == "function" && obj(e)
      } else {
        a.alert(t.msg)
      }
    }, true)
  },
  /**
   * 砍价详情
   */
  kj_info: function (id, uid, that, obj) {
    var e = {};
    a.get("Bargain/BargainInfo", {
      id: id,
      user_id: uid
    }, function (t) {
      if (t.code == 0 && t.info != null) {
        s.wxParse("wxParseData", "html", t.info.bargain_info.activity_rules, that, "0");
        e.bargain_info = t.info.bargain_info;
        e.bargain_info.cons_time = a.time_format(t.info.bargain_info.consumption_time);
        e.user_info = t.info.user_info;
        e.show = true;
        var now_time = Date.parse(new Date()) / 1000;
        if (now_time > t.info.bargain_info.consumption_time) {
          e.overtime = 1;//已过期
        } else {
          e.overtime = 2
        }
        wx.setNavigationBarTitle({
          title: t.info.bargain_info.bargain_name || "砍价详情"
        })
        typeof obj == "function" && obj(e)
      } else {
        a.alert(t.msg)
        setTimeout(function () {
          wx.navigateBack()
        }, 1e3)
      }
    }, true)
  },
  /**
   * 发起砍价
   * param id 活动id
   * param status 是否提示
   * return object
   */
  BargainCreate: function (id, obj, status) {
    a.get('Bargain/BargainCreate', {
      bargain_id: id,
      user_id: getApp().getCache("userinfo").uid
    }, function (t) {
      console.log(t)
      if (t.code == 0) {
        t.popup = true;
      } else {
        if (!status) {
          a.alert(t.msg)
        }
      }
      typeof obj == "function" && obj(t) //注意：特殊需求，勿动
    })
  },
  /**
   * 帮他砍价
   */
  BargainHelp: function (id, uid,formId,obj) {
    a.get('Bargain/BargainHelp', {
      iInitiated_id: id,
      user_id: getApp().getCache("userinfo").uid,
      uid:uid  
    }, function (t) {
      if (t.code == 0) {
        console.log(formId,'101');
        a.get('Wxpush/BargainHelpPush', {
          id: id,
          user_id: getApp().getCache("userinfo").uid,
          formid: formId
        }, function (t) {
          console.log(t)
        })
        t.popup = true;
        typeof obj == "function" && obj(t)
      } else {
          a.alert(t.msg)
      }
    })
  },
  /**
   * 帮砍记录
   */
  BargainRecord: function (id, obj) {
    var e = {};
    a.get("Bargain/BargainRecord", {
      iInitiated_id: id
    }, function (t) {
      if (t.code == 0) {
        t.info.forEach(function (t) {
          t.create_time = a.time_format(t.create_time);
          if (t.user_id == getApp().getCache("userinfo").uid) {
            t.nick_name = "系统";
            t.user_headimg = "/yb_mingpian/static/images/icon/kj_info.png";
          }
        })
        e.list = t.info;
        e.show = true;
        typeof obj == "function" && obj(e)
      } else {
        a.alert(t.msg)
      }
    }, true)
  },
  /**
  * 我的砍价记录
  */
  MyBargain: function (page, that, obj) {
    var e = {};
    a.get("Bargain/MyBargain", {
      page: page,
      user_id: getApp().getCache("userinfo").uid
    }, function (t) {
      if (t.code == 0) {
        t.info.forEach(function (i) {
          var now_time = Date.parse(new Date()) / 1000;
          if (now_time >i.consumption_time) {
            i.overtime = 1;//已过期
          } else {
            i.overtime = 2
          }
          if (i.end_time > now_time) {
            i.goods_type = 1  //进行中
          } else {
            i.goods_type = 2 //已结束
          }
          if (i.current_price - i.lowest_price <= 0) {
            i.kj_type = 1  //砍价已完成
          } else {
            i.kj_type = 2  //砍价未完成
          }
        })
        t.info.length > 0 && (e.page = page + 1, e.list = that.data.list.concat(t.info), t.info.length < 10 && (e.loaded = true));
        t.info.length == 0 && (e.loaded = true);
        e.show = true;
        typeof obj == "function" && obj(e)
      } else {
        a.alert(t.msg)
      }
    }, true)
  },
  /**
   * 倒计时
   */
  Countdown: function (time, obj) {
    var e = {},
      totalSecond = time - Date.parse(new Date()) / 1000;
    if (totalSecond <= 0) {
      e.show_time = false;
      typeof obj == "function" & obj(e)
    }else{
      var interval = setInterval(function () {
        // 秒数  
        var second = totalSecond;

        // 天数位  
        var day = Math.floor(second / 3600 / 24);
        var dayStr = day.toString();
        if (dayStr.length == 1) dayStr = '0' + dayStr;

        // 小时位  
        var hr = Math.floor((second - day * 3600 * 24) / 3600);
        var hrStr = hr.toString();
        if (hrStr.length == 1) hrStr = '0' + hrStr;

        // 分钟位  
        var min = Math.floor((second - day * 3600 * 24 - hr * 3600) / 60);
        var minStr = min.toString();
        if (minStr.length == 1) minStr = '0' + minStr;

        // 秒位  
        var sec = second - day * 3600 * 24 - hr * 3600 - min * 60;
        var secStr = sec.toString();
        if (secStr.length == 1) secStr = '0' + secStr;
        e.countDownDay = dayStr,
          e.countDownHour = hrStr,
          e.countDownMinute = minStr,
          e.countDownSecond = secStr,
          typeof obj == "function" && obj(e)
        totalSecond--;
        if (totalSecond < 0) {
          clearInterval(interval);
          wx.showToast({
            title: '活动已结束',
          });
          e.countDownDay = "0",
            e.countDownHour = "0",
            e.countDownMinute = "0",
            e.countDownSecond = "0",
            e.show_time = false;
          typeof obj == "function" && obj(e)
        }
      }.bind(this), 1000);
    }

  },
  /**
   * 定义函数removeByValue进行元素删除
   * param arr 数组 
   * param val 元素value值
   * return arr
   */
  removeByValue: function (arr, val, obj) {
    var index = -1;
    for (var i = 0; i < arr.length; i++) {
      console.log(arr[i])
      if (arr[i] == val) {
        index = i;
        break;
      }
    }
    arr.splice(index, 1);
    typeof obj == "function" && obj(arr)
  }
  
}
